package com.cyf.util;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 短信工具类
 * Created by cage on 2017/2/15.
 */
public class SmsUtils {

    private static String ENCODING = "UTF-8";
    // 智能匹配模板发送接口的http地址
    private static String URI_SEND_SMS = "https://sms.yunpian.com/v2/sms/single_send.json";

    private static String apikey = "b2ab6b83cfc416dcfb2ce981d59445d8";

    public static int getCode() {
        Random rand = new Random();
        int tmp = Math.abs(rand.nextInt());
        return tmp % (999999 - 100000 + 1) + 100000;
    }

    public static String getTxt(Integer code) {
        return "【车易付】车易付验证码:" + code.toString() + "(20分钟内有效，如您已成功注册，请忽略此条信息)";
    }

    public static String getTxtForApp(Integer code) {
        return "【易停宝】您的验证码是"+ code +"。如非本人操作，请忽略本短信";
    }

    /**
     * 访客系统：受访者邀请访客，发送短信通知
     * @param mobile
     * @return
     */
    public static String getTxtForVisitorSystem(String mobile) {
        return "【易停宝】您的验证码是"+ mobile +"。如非本人操作，请忽略本短信";
    }


    public static String SendSms(String txt, String mobile) throws IOException {
        Map<String, String> params = new HashMap<String, String>();
        params.put("apikey", apikey);
        params.put("text", txt);
        params.put("mobile", mobile);
        return HttpsUtil.postText(URI_SEND_SMS, params);
    }

    /**
     * 获取收入通知
     * @param shouldfee     应收（分）
     * @param actualfee     实收（分）
     * @param date      日期
     * @param parkingName   车场名称
     * @return  String
     */
    public static String getFeeFlowSmsTxt(int shouldfee, int actualfee, Date date, String parkingName) {
        if (parkingName.endsWith("车场")) {
            parkingName = parkingName.substring(0, parkingName.length() - 2);
        }
        DecimalFormat decimalFormat = new DecimalFormat("0.00");
        String shouldfeeStr = decimalFormat.format(shouldfee * 0.01);
        String actualfeeStr = decimalFormat.format(actualfee * 0.01);
        String dateStr = DateUtil.formatDate(date, "yyyy-MM-dd");
        return "尊敬的领导，"+ parkingName +"车场"+ dateStr +"应收"+ shouldfeeStr +"元，实收"+ actualfeeStr +"元。";
    }
}
